package com.fr.base;

import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.util.Utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

public abstract class XMLTools
{
  public static void readInputStreamXML(XMLReadable paramXMLReadable, InputStream paramInputStream)
    throws Exception
  {
    InputStreamReader localInputStreamReader = new InputStreamReader(paramInputStream, "UTF8");
    XMLableReader localXMLableReader = XMLableReader.createXMLableReader(localInputStreamReader);
    if (localXMLableReader != null)
      localXMLableReader.readXMLObject(paramXMLReadable);
  }

  public static void readFileXML(XMLReadable paramXMLReadable, File paramFile)
    throws Exception
  {
    if ((paramFile == null) || (!(paramFile.exists())))
      return;
    FileInputStream localFileInputStream = new FileInputStream(paramFile);
    readInputStreamXML(paramXMLReadable, localFileInputStream);
    localFileInputStream.close();
  }

  public static void writeOutputStreamXML(XMLWriter paramXMLWriter, OutputStream paramOutputStream)
  {
    XMLPrintWriter localXMLPrintWriter = XMLPrintWriter.create(paramOutputStream);
    paramXMLWriter.writeXML(localXMLPrintWriter);
    localXMLPrintWriter.flush();
    localXMLPrintWriter.close();
  }

  public static void writeFileXML(XMLWriter paramXMLWriter, File paramFile)
  {
    if (paramFile == null)
      return;
    if (!(paramFile.getParentFile().exists()))
      Utils.mkdirs(paramFile.getParentFile());
    try
    {
      for (File localFile = paramFile; localFile.exists(); localFile = new File(localFile.getParentFile(), localFile.getName() + ".1"));
      FileOutputStream localFileOutputStream = new FileOutputStream(localFile);
      writeOutputStreamXML(paramXMLWriter, localFileOutputStream);
      localFileOutputStream.flush();
      localFileOutputStream.close();
      if (localFile != paramFile)
      {
        if (paramFile.exists())
          paramFile.delete();
        localFile.renameTo(paramFile);
      }
    }
    catch (IOException localIOException)
    {
      FRContext.getLogger().log(Level.SEVERE, "Write XML file: " + paramFile + "\n" + localIOException.getMessage(), localIOException);
    }
  }
}